package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.StoreFollow;
import com.dingreading.cloud.admin.entity.table.StoreFollowTableDef;
import com.dingreading.cloud.admin.mapper.StoreFollowMapper;
import com.dingreading.cloud.admin.service.StoreFollowService;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-11-19
 */
@Service
public class StoreFollowServiceImpl extends BaseServiceImpl<StoreFollowMapper, StoreFollow, StoreFollowTableDef> implements StoreFollowService {

    @Override
    protected StoreFollowTableDef getTable() {
        return StoreFollowTableDef.storeFollow;
    }

    @Override
    public Page<StoreFollow> pageList(PageUtil pageUtil, String storeUid, String beginDate, String endDate, String keyword, Integer source, Integer operate) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.content.like(keyword)
                    .or(table.followTypeName.like(keyword))
            );
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.followTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));
        if (source != null)
            condition.and(table.source.eq(source));
        if (operate != null)
            condition.and(table.operation.eq(operate));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginateWithRelations(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public List<StoreFollow> listByStoreUid(String storeUid, Integer source, Integer operate) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid));
        if (source != null)
            condition.and(table.source.eq(source));
        if (operate != null)
            condition.and(table.operation.eq(operate));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public List<StoreFollow> listByStoreUidAndOperate(String storeUid, Integer operate) {
        return getMapper().selectListByCondition(table.storeUid.eq(storeUid).and(table.operation.eq(operate)));
    }

    @Override
    public boolean used(String uid) {
        return exists(table.followTypeUid.eq(uid));
    }

}
